package com.eportal.controller.background;

import com.alibaba.fastjson.JSONObject;
import com.eportal.action.background.FuncMainAction;
import com.eportal.domain.Sysopt;
import com.eportal.service.FuncMainService;
import com.eportal.vo.controller.background.funcmain.FuncMain4AgentListVO;
import com.eportal.vo.controller.background.funcmain.ViewDetailVO;
import com.framework.jqgrid.Page;
import com.framework.jqgrid.PageEntity;
import com.github.pagehelper.PageInfo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * 缴费开通管理
 * Created by Saber on 2017/8/24.
 */
@Controller
@RequestMapping(value = "/admin/funcMain")
public class FuncMainController {

    private static Logger logger = LoggerFactory.getLogger(TOpenLogController.class.getName());

    @Resource
    private FuncMainService funcMainService;

    @RequestMapping(value = "page.json")
    @ResponseBody
    public JSONObject page(HttpServletRequest request,PageEntity pageEntity){

        JSONObject json = new JSONObject();

        Sysopt user= (Sysopt)request.getSession().getAttribute("user");
        int userRole = (Integer)request.getSession().getAttribute("userRole");

        try {
            PageInfo<FuncMain4AgentListVO> pageInfo=funcMainService.queryForAgentPage(pageEntity, user.getAgtCode(),user, userRole);

            json.put("total",pageInfo.getTotal());

            if(pageInfo.getList().size()==0){
                json.put("msg","暂无数据");
            }else{
                json.put("msg","ok");
            }
            json.put("data",pageInfo.getList());
            Page page = new Page(pageInfo,pageEntity.getPageNo());
            json.put("page", page);
            json.put("code",1);
            return json;
        }catch (Exception e){
            e.printStackTrace();
            logger.error("门禁管理列表异常:",e);
            json.put("code", 0);
            json.put("msg","服务器发生异常:"+e.getMessage());
            json.put("data",null);
            return json;
        }

    }

    /**
     * 缴费申请详情
     * @author Saber
     * @Date 2017/8/24 下午12:59
     * @param request
     * @param sId
     * @see FuncMainAction#viewDetail()
     * @return
     */
    @RequestMapping(value = "viewDetail.json")
    @ResponseBody
    public JSONObject viewDetail(HttpServletRequest request,Long sId){
        JSONObject json = new JSONObject();

        Sysopt user= (Sysopt)request.getSession().getAttribute("user");
        int userRole = (Integer)request.getSession().getAttribute("userRole");

        try {
            List<ViewDetailVO> list = funcMainService.listDetail(sId,user.getAgtCode(), user, userRole);
            json.put("code",1);
            json.put("msg","ok");
            json.put("data",list);
            return json;
        }catch (Exception e){
            e.printStackTrace();
            logger.error("获取缴费申请详情异常:",e);
            json.put("code",0);
            json.put("msg","服务器发生异常:"+e.getMessage());
            json.put("data",null);
            return json;
        }
    }

    /**
     * 缴费开通申请详情2（申请时使用）
     * @param request
     * @param sId
     * @see FuncMainAction#applyView()
     * @return
     */
    @RequestMapping(value = "applyDetail.json")
    @ResponseBody
    public JSONObject applyDetail(HttpServletRequest request,Long sId){
        JSONObject json = new JSONObject();

        Sysopt user= (Sysopt)request.getSession().getAttribute("user");
        int userRole = (Integer)request.getSession().getAttribute("userRole");

        try {
            json= funcMainService.applyDetail(sId, user, userRole);
            return json;
        }catch (Exception e){
            e.printStackTrace();
            logger.error("获取缴费申请详情2异常:",e);
            json.put("code",0);
            json.put("msg","服务器发生异常:"+e.getMessage());
            json.put("data",null);
            return json;
        }
    }

    /**
     * 缴费开通申请
     * @author Saber
     * @Date 2017/8/25 上午9:32
     * @param request
     * @param sId               sId
     * @param communityIds      小区id集合 用,分割
     * @param alipayIds         支付宝账号id集合 用,分割  （FEE_ALIPAY_AUTH表sId）
     * @see FuncMainAction#apply()
     * @return
     */
    @RequestMapping(value = "apply.json",method = RequestMethod.POST)
    @ResponseBody
    public JSONObject apply(HttpServletRequest request,Long sId,String communityIds,String alipayIds){
        JSONObject json = new JSONObject();

        Sysopt user= (Sysopt)request.getSession().getAttribute("user");
        int userRole = (Integer)request.getSession().getAttribute("userRole");

        try {
            json= funcMainService.apply(sId,communityIds,alipayIds, user.getAgtCode(), user.getSid());
            return json;
        }catch (Exception e){
            e.printStackTrace();
            logger.error("获取缴费申请详情2异常:",e);
            json.put("code",0);
            json.put("msg","服务器发生异常:"+e.getMessage());
            json.put("data",null);
            return json;
        }
    }
}
